//@charset "utf-8"
/**
 * 建立replaceAll 全部替换函数
 * @param type string strFrom 搜索需要替换的字符
 * @param type string strTo 替换成什么
 */
String.prototype.replaceAll = function(strFrom, strTo){
	return this.replace(new RegExp(strFrom, 'gm'), strTo);
}

/**
 * 统一调用客户端接口
 *
 * @author	汪平平<wangpingping@myhexin.com>
 * @create	2014/12/06
 * @dependence	原生js，不依赖其他js框架
 * @notic
 */
function ComClientInterface() {
	//方法不存在提示
	this.noenvironment = "the client environment does not exist!";

	/**
	 * 设置预警添加接口数据格式
	 */
	//设置预警参数格式 content传递的是json样式的字符串[{},{},{},{}]
	this.setSubTemplateData = {
		"mobileid": "",
		"pcid": "",
		"code": "",
		"content": ""
	};
	//设置预警参数里 每项条件的参数格式，最终所有条件存入数组
	this.setSubEveryTempData = '{"mobileid":"","pcid":"","datainfo":';
	this.setSubEveryTempData += '{"type":"2","stockcode":"","stockname":"","market":"","userid":"","templates":"","value":"","mode":""},';
	this.setSubEveryTempData += '"platform":"pc",';
	this.setSubEveryTempData += '"appname":"hexin"}';

	this._init.apply(this, arguments);
}

ComClientInterface.prototype = {
	_init: function (options) {
		options = options || {};
		//客户端以及手机端标识
		this.mobileid = options.mobileid || '';
		this.pcid = options.pcid || '';

		//单次操作的股票代码以及名称
		this.stockcode = options.stockcode || '';
		this.stockname = options.stockname || '';

		this.setSubTemplateData['mobileid'] = this.mobileid;
		this.setSubTemplateData['pcid'] = this.pcid;
		//this.setSubTemplateData['code'] = this.stockcode;

		//设置预警的数据，json格式
		this.setSubData = new Array();

		//错误标记为空
		this.error = false;
		this.errormsg = '';
		//警告标记为空
		this.waring = false;
		this.waringmsg = '';

		try {
			this.clientUtil = external.createObject("Util");
			this.fis = external.createObject('FileInputStream');
        	this.fos = external.createObject('FileOutputStream');
			return true;
		} catch (e) {
			return this._error(this.noenvironment);
		}
	},
	_throw: function () {
		//抛出内容
		if (typeof(console) !== 'undefined') {
			if (arguments.length == 1) {
				console.log(arguments[0]);
			} else if (arguments.length > 1) {
				console.log(arguments);
			}
		} else {
			if (arguments.length == 1) {
				alert(arguments[0]);
			} else if (arguments.length > 1) {
				for (var i = 0; i < arguments.length; i ++)
					alert("第" + (i+1) + "个输出：" + arguments[i]);
			}
		}
	},
	_error: function (msg) {
		this._throw(msg);

		this.error = true;
		this.errormsg = msg;

		return false;
	},
	_waring: function (msg) {
		this._throw(msg);

		this.waring = true;
		this.waringmsg = msg;
	},
	getErrorMsg: function () {
		return this.errormsg;
	},
	getWaringMsg: function () {
		return this.waringmsg;
	},

	/**
	 * 设置预警条件
	 * @param string param 预警设置的项
	 * @param string value 预警设置项的值
	 */
	decToHex: function(str) {
		var res = [];
		for (var i = 0; i < str.length; i++)
			res[i] = ("00" + str.charCodeAt(i).toString(16)).slice(-4);
		return "\\u" + res.join("\\u");
	},
	formatSetSubData: function (param, value, mode,market, pcid, mobileid) {
		var subscribeData = this.setSubEveryTempData;
		param = param || '';
		value = value || '';
		mode = mode || '';
		market = market || '';
		pcid = pcid || '';
		mobileid = mobileid || '';
		if (param != '' && value != '' ) {
			var stockNameUni = this.decToHex(this.stockname);
			subscribeData = subscribeData.replaceAll('"stockcode":""', '"stockcode":"' + this.stockcode + '"');
			subscribeData = subscribeData.replaceAll('"stockname":""', '"stockname":"' + stockNameUni + '"');
			subscribeData = subscribeData.replaceAll('"templates":""', '"templates":"' + param + '"');
			subscribeData = subscribeData.replaceAll('"market":""', '"market":"' + market + '"');
			subscribeData = subscribeData.replaceAll('"value":""', '"value":"' + value + '"');
			subscribeData = subscribeData.replaceAll('"mode":""', '"mode":"' + mode + '"');
			if(pcid != ''){
				subscribeData = subscribeData.replaceAll('"pcid":""', '"pcid":"' + pcid + '"');
			}else if(mobileid != ''){
				subscribeData = subscribeData.replaceAll('"mobileid":""', '"mobileid":"' + mobileid + '"');
			}
		}
		//alert(subscribeData+"aaaaaaaaa");
		this.setSubData.push(subscribeData);

	},
	//调用设置预警接口
	postSetSubData: function (pcc) {
		var postSetData = this.setSubTemplateData;
		pcc = pcc || '';
		postSetData['content'] = '[' + this.setSubData.join(',') + ']';
		// alert('post'+postSetData['content']);
		var truestr = this.clientUtil.setAlertCfg(postSetData);
		if(truestr == true){
			if(pcc != ''){
				/*$(".kinds_tip").html('修改成功！');
		        $(".kinds_tip").fadeIn(500);
		        clearTimeout(time);
		        var time = setTimeout(function(){
		           $(".kinds_tip").fadeOut(500);
		        },800);*/
			}else{
				$(".kinds_tip").html('设置成功！');
		        $(".kinds_tip").fadeIn(500);
		        clearTimeout(time);
		        var time = setTimeout(function(){
		           external.closeWindow();
		           $(".kinds_tip").fadeOut(500);
		        },800);
			}

		}else{
			$(".kinds_tip").html('设置失败！');
	        $(".kinds_tip").fadeIn(500);
	        clearTimeout(time);
	        var time = setTimeout(function(){
	           $(".kinds_tip").fadeOut(500);
	        },800);
		}
	},

	/**
	 * 获取客户端预警条件
	 * @param string code 股票代码
	 */
	getSubData: function (code,market) {
		code = code || '';
		market = market || '';
		var thisClientInfo = {'mobileid': '', 'pcid': '', 'code': code,'market':market};
		if(code == ''){
			return this.clientUtil.getAlertCfg();
		}
		return this.clientUtil.getAlertCfg(thisClientInfo);
	},

	/**
	 * 获取预警结果内容获取第M-N条结果//M<=N 且，M,N 以0为起始序号
	 */
	getSubResult: function () {
		return this.clientUtil.getAlertResult({
			'start': M,
			'end':N
		});
	},
	/*获取本地文件的内容*/
	getFileContent : function (file) {
		var userPath = external.getUserPath();
		if (null === this.fis) {
                return false;
            }
            var str = false;
            if (this.fis.open(userPath+file)) {
                try {
                    str = this.fis.read();
                } catch (exp) {
                    //messageView.notice(exp.message);
                }

                this.fis.close();
            }

            if (false === str) {
                return false;
            }

            return eval('('+str+')');
	},
	/*写入文件*/
	writeFileContent : function (file, jsonObj) {
		var userPath = external.getUserPath();
		var str = '';
            if (null === this.fos) {
                return false;
            }

            if (this.fos.open(userPath+file)) {
                try {
                    str = this.fos.write(jsonObj);
                } catch (exp) {
                    //alert(exp.message);
                }

                this.fos.close();
            }
	},


	/**
	 * 获取预警结果条数
	 */
	getAlertResultCount: function () {
		return this.clientUtil.getAlertResultCount();
	},
	/**
	 * 获取客户端预警提示声音
	 * @param 返回结果: 预警声音的名称,不含后缀. 多个声音之间逗号隔开. 如"alert,向上,清脆"
	 */
	getAlertSounds: function () {
		return this.clientUtil.getAlertSounds();
	},
	/**
	 * 删除预警结果
	 * @param string moveStr 关闭对应的标识，格式为 "mobileid : xxx, pcid : xxx, mobileid : xxx, mobileid : xxx ,..." 只填非空的那一项id
	 */
	moveSubResult: function (moveStr) {
		moveStr = moveStr || '';
		return this.clientUtil.removeAlertResult(moveStr);
	},

	/**
	 * 设置默认预警方式
	 * @param string
	 */
	setDefaultAlertMode: function (modeStr) {
		return this.clientUtil.setDefaultAlertMode(modeStr);
	},
	/**
	 * 获取默认预警方式
	 * @param
	 */
	getDefaultAlertMode: function () {
		return this.clientUtil.getDefaultAlertMode();
	},
	/**
	 * 预警条件达到以后绑定事件
	 * @param function fnName 方法名称
	 */
	setRegisterAlertPush: function (fnName) {
		fnName = fnName || '';
		if (fnName == '') {
			//return this._error(this.noenvironment);
		}
		return this.clientUtil.registerAlertPush(fnName);
	},

	/**
	 * 关闭预警
	 * @param string closeStr 关闭对应的标识，格式为 "mobileid : xxx, pcid : xxx, mobileid : xxx, mobileid : xxx ,..." 只填非空的那一项id
	 */
	closeSub: function (closeStr) {
		closeStr = closeStr || '';
		return this.clientUtil.closeAlert(closeStr);
	},

	/**
	 * 打开预警
	 * @param string openStr 关闭对应的标识，格式为 "mobileid : xxx, pcid : xxx, mobileid : xxx, mobileid : xxx ,..." 只填非空的那一项id
	 */
	openSub: function (openStr) {
		openStr = openStr || '';
		return this.clientUtil.openAlert(openStr);
	},

	/**
	 * 删除预警
	 * @param string delStr 删除对应的标识，格式为 "mobileid : xxx, pcid : xxx, mobileid : xxx, mobileid : xxx ,..." 只填非空的那一项id
	 */
	delsubscribe: function (delStr) {
		delStr = delStr || '';
		return this.clientUtil.delAlertCfg(delStr);
	},
	/**
	 * 其他条件
	 * @param string code
	 * openAlertSetDlg(string)
	 */
	openotherAlert: function (code,market) {
		code = code || '';
		market = market || '';
		var options = {'code': code, 'market': market};
		return this.clientUtil.openAlertSetDlg(options);
	},
	/**
	 * 导出
	 * @param int type 导出格式  0: excel 1.txt 2.剪贴板
	 * @param string content 导出的内容
	 */
	exportSubContent: function (type, content) {
		type = type || 0;
		content = content || '';
		var expertData = {'type': type, 'content': content};
		return this.clientUtil.exportTo(expertData);
	},

	/**
	 * 修改窗口大小
	 * @param int width 宽
	 * @param int height 高
	 * @param int moveStyle 框的位置
	 */
	modifyWindowsSize: function (width, height,moveStyle) {
		external.SetWindowRect({
			'width' : width,
			'height' : height,
			"moveStyle":"Center"
		});
	},
	/**
	 * 修改窗口大小
	 * @param int width 宽
	 * @param int height 高
	 */
	modifyWindowsSizeL: function (width, height) {
		external.SetWindowRect({
			'width' : width,
			'height' : height
		});
	},
	/**
	 * 打开老的预警界面
	 * @param string code 股票代码
	 */
	openAlertSetDlg: function (code) {
		return this.clientUtil.openAlertSetDlg(code);
	},
	/**
	 * 打开客户端公式
	 * @param string pcid
	 */
	modLocalAlertCfg: function (pcid) {
		return this.clientUtil.modLocalAlertCfg(pcid);
	},
	/**
	 * 设置预警结果为已读
	 * @param string read
	 */
	setAlertResultRead: function (read) {
		return this.clientUtil.setAlertResultRead(read);
	},
	/**
	 * 加入到板块
	 * @param string codes 股票代码 多个股票之间用半角分号“;”分开
	 */
	addCodeToBlock: function (codes) {
		return this.clientUtil.addCodeToBlock(codes);
	},
	/**
	 * 打开资讯弹窗
	 * @param url,1
	 */
	openURL: function (url) {
		return this.clientUtil.openURL(url,1);
	},

	/**
	 * 打开预警设置的浏览器
	 * @param string code 股票代码
	 */
	openBrownSetSub: function (code,market) {
		code = code || '';
		market = market || '';
		var options = {'code': code, 'market': market};
		return this.clientUtil.openAlertSetWebDlg(options);
	},

	/**
	 * 打开预警结果的浏览器
	 * @param string code 股票代码
	 */
	openBrownSubResult: function (code) {
		return this.clientUtil.openAlertResultWebDlg(code);
	},
	/**
	 * 打开问财预警
	 * @param string code 股票代码
	 */
	openAskEarlyWebDlg: function (code) {
		return this.clientUtil.openAskEarlyWebDlg(code);
	},
	/**
	 * 打开问财预警结果页面
	 * @param string url
	 */
	ShowDetailWebDlg: function (url) {
		return this.clientUtil.ShowDetailWebDlg(url);
	}

};